package com.core.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JComboBox;

import com.core.dao.impl.CityDAOImpl;
import com.core.dao.impl.ContactDAOImpl;
import com.core.dao.impl.CountryDAOImpl;
import com.core.dao.impl.Online_accountDAOImpl;
import com.core.dao.impl.StateDAOImpl;
import com.core.dao.impl.UsernameDAOImpl;
import com.core.dao.impl.WorkDAOImpl;
import com.core.database.Manager;
import com.core.entities.City;
import com.core.entities.Contact;
import com.core.entities.Country;
import com.core.entities.Online_account;
import com.core.entities.State;
import com.core.entities.Username;
import com.core.entities.Work;
import com.core.exception.DAOException;
import com.core.gui.components.CustomComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AddContact extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private final JLabel lblNewLabel = new JLabel("First Name :");
	private final JTextField firstNameTextField = new JTextField();
	private final JLabel lblSecondName = new JLabel("Second Name :");
	private final JTextField secondNameTextField = new JTextField();
	private final JLabel lblThirdName = new JLabel("Third Name :");
	private final JTextField thirdNameTextField = new JTextField();
	private final JLabel lblLastName = new JLabel("Last Name :");
	private final JTextField lastNameTextField = new JTextField();
	private final JLabel lblAddress = new JLabel("Address :");
	private final JTextArea addressTextArea = new JTextArea();
	private final JLabel lblCountry = new JLabel("Country :");
	private final CustomComboBox<Country> countryComboBox = new CustomComboBox<Country>();
	private final JLabel lblStat = new JLabel("Stat :");
	private final CustomComboBox<State> stateComboBox = new CustomComboBox<State>();
	private final JLabel lblCity = new JLabel("City :");
	private final CustomComboBox<City> cityComboBox = new CustomComboBox<City>();
	private final JLabel lblNewLabel_1 = new JLabel("Land Phone :");
	private final JTextField landPhoneTextField = new JTextField();
	private final JLabel lblMobilePhone = new JLabel("Mobile Phone :");
	private final JTextField mobilePhoneTextField = new JTextField();
	private final JLabel lblEmail = new JLabel("E-Mail :");
	private final JTextField emailTextField = new JTextField();
	private final JLabel lblWebsite = new JLabel("Website :");
	private final JTextField websiteTextField = new JTextField();
	private final JLabel lblFacebook = new JLabel("Facebook :");
	private final JTextField facebookTextField = new JTextField();
	private final JLabel lblNewLabel_2 = new JLabel("confirm");
	private final JLabel lblNewLabel_3 = new JLabel("Twitter :");
	private final JTextField twitterTextField = new JTextField();
	private final JLabel lblConfirm = new JLabel(" confirm");
	private final JLabel lblGoogle = new JLabel("Google :");
	private final JTextField googleTextField = new JTextField();
	private final JLabel label = new JLabel(" confirm");
	private final JLabel lblJob = new JLabel("Job :");
	private final JTextField jobTextField = new JTextField();
	private final JLabel lblCompanyName = new JLabel("Company Name :");
	private final JTextField companyTextField = new JTextField();
	private final JLabel lblWorkLandPhone = new JLabel("Work Land Phone :");
	private final JTextField workLandPhoneTextField = new JTextField();
	private final JLabel lblWorkMobilePhone = new JLabel("Work Mobile Phone :");
	private final JTextField workMobilePhoneTextField = new JTextField();
	private final JLabel lblWorkEmail = new JLabel("Work E-Mail :");
	private final JTextField workEmailTextField = new JTextField();
	private final JLabel lblWorkWebsite = new JLabel("Work Website :");
	private final JTextField workWebsiteTextField = new JTextField();
	private final JLabel lblWorkCountry = new JLabel("Work Country :");
	private final CustomComboBox<Country> workCountryComboBox = new CustomComboBox<Country>();
	private final JLabel lblWorkState = new JLabel("Work State :");
	private final CustomComboBox<State> workStateComboBox = new CustomComboBox<State>();
	private final JLabel lblWorkCity = new JLabel("Work City  :");
	private final CustomComboBox<City> workCityComboBox = new CustomComboBox<City>();
	private final JTextField workGoogleTextField = new JTextField();
	private final JTextField workTwitterTextField = new JTextField();
	private final JTextField workFacebookTextField = new JTextField();
	private final JLabel lblWorkFacebook = new JLabel("Work Facebook :");
	private final JLabel lblWorkTwitter = new JLabel("Work Twitter :");
	private final JLabel lblWorkGoogle = new JLabel("Work Google :");
	private final JLabel label_4 = new JLabel(" confirm");
	private final JLabel label_5 = new JLabel(" confirm");
	private final JLabel label_6 = new JLabel("confirm");

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			AddContact dialog = new AddContact();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	public AddContact() {
		initComponents() ;
		workWebsiteTextField.setBounds(87, 367, 245, 20);
		workWebsiteTextField.setColumns(10);
		workEmailTextField.setBounds(83, 342, 249, 20);
		workEmailTextField.setColumns(10);
		workMobilePhoneTextField.setBounds(116, 317, 194, 20);
		workMobilePhoneTextField.setColumns(10);
		workLandPhoneTextField.setBounds(106, 292, 194, 20);
		workLandPhoneTextField.setColumns(10);
		companyTextField.setBounds(96, 267, 236, 20);
		companyTextField.setColumns(10);
		jobTextField.setBounds(35, 239, 281, 20);
		jobTextField.setColumns(10);
		googleTextField.setBounds(60, 214, 272, 20);
		googleTextField.setColumns(10);
		twitterTextField.setBounds(60, 189, 272, 20);
		twitterTextField.setColumns(10);
		facebookTextField.setBounds(68, 164, 264, 20);
		facebookTextField.setColumns(10);
		websiteTextField.setBounds(58, 139, 274, 20);
		websiteTextField.setColumns(10);
		emailTextField.setBounds(48, 114, 281, 20);
		emailTextField.setColumns(10);
		mobilePhoneTextField.setBounds(329, 89, 168, 20);
		mobilePhoneTextField.setColumns(10);
		landPhoneTextField.setBounds(83, 89, 168, 20);
		landPhoneTextField.setColumns(10);
		lastNameTextField.setBounds(601, 8, 99, 20);
		lastNameTextField.setColumns(10);
		thirdNameTextField.setBounds(428, 8, 105, 20);
		thirdNameTextField.setColumns(10);
		secondNameTextField.setBounds(256, 8, 99, 20);
		secondNameTextField.setColumns(10);
		firstNameTextField.setBounds(70, 8, 99, 20);
		firstNameTextField.setColumns(10);
		setBounds(100, 100, 758, 582);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lblNewLabel.setBounds(10, 11, 58, 14);

		contentPanel.add(lblNewLabel);

		contentPanel.add(firstNameTextField);
		lblSecondName.setBounds(179, 11, 72, 14);

		contentPanel.add(lblSecondName);

		contentPanel.add(secondNameTextField);
		lblThirdName.setBounds(365, 11, 61, 14);

		contentPanel.add(lblThirdName);

		contentPanel.add(thirdNameTextField);
		lblLastName.setBounds(543, 11, 57, 14);

		contentPanel.add(lblLastName);

		contentPanel.add(lastNameTextField);
		lblAddress.setBounds(10, 36, 46, 14);

		contentPanel.add(lblAddress);
		addressTextArea.setBounds(60, 36, 235, 37);

		contentPanel.add(addressTextArea);
		lblCountry.setBounds(305, 39, 46, 14);
		{
		
		}
		contentPanel.add(lblCountry);


		countryComboBox.setBounds(353, 33, 152, 20);

		contentPanel.add(countryComboBox);
		lblStat.setBounds(515, 36, 27, 14);

		contentPanel.add(lblStat);

		stateComboBox.setBounds(543, 33, 157, 20);

		contentPanel.add(stateComboBox);
		lblCity.setBounds(305, 64, 27, 14);

		contentPanel.add(lblCity);

		cityComboBox.setBounds(334, 61, 171, 20);

		contentPanel.add(cityComboBox);
		lblNewLabel_1.setBounds(10, 92, 63, 14);

		contentPanel.add(lblNewLabel_1);

		contentPanel.add(landPhoneTextField);
		lblMobilePhone.setBounds(256, 92, 70, 14);

		contentPanel.add(lblMobilePhone);

		contentPanel.add(mobilePhoneTextField);
		lblEmail.setBounds(10, 117, 35, 14);

		contentPanel.add(lblEmail);

		contentPanel.add(emailTextField);
		lblWebsite.setBounds(10, 142, 46, 14);

		contentPanel.add(lblWebsite);

		contentPanel.add(websiteTextField);
		lblFacebook.setBounds(10, 167, 53, 14);

		contentPanel.add(lblFacebook);

		contentPanel.add(facebookTextField);
		lblNewLabel_2.setBounds(336, 167, 46, 14);

		contentPanel.add(lblNewLabel_2);
		lblNewLabel_3.setBounds(10, 192, 46, 14);

		contentPanel.add(lblNewLabel_3);

		contentPanel.add(twitterTextField);
		lblConfirm.setBounds(334, 192, 46, 14);

		contentPanel.add(lblConfirm);
		lblGoogle.setBounds(10, 217, 40, 14);

		contentPanel.add(lblGoogle);

		contentPanel.add(googleTextField);
		label.setBounds(334, 217, 46, 14);

		contentPanel.add(label);
		lblJob.setBounds(10, 242, 27, 14);

		contentPanel.add(lblJob);

		contentPanel.add(jobTextField);
		lblCompanyName.setBounds(10, 270, 82, 14);

		contentPanel.add(lblCompanyName);

		contentPanel.add(companyTextField);
		lblWorkLandPhone.setBounds(10, 295, 91, 14);

		contentPanel.add(lblWorkLandPhone);

		contentPanel.add(workLandPhoneTextField);
		lblWorkMobilePhone.setBounds(10, 320, 98, 14);

		contentPanel.add(lblWorkMobilePhone);

		contentPanel.add(workMobilePhoneTextField);
		lblWorkEmail.setBounds(10, 345, 63, 14);

		contentPanel.add(lblWorkEmail);

		contentPanel.add(workEmailTextField);
		lblWorkWebsite.setBounds(10, 370, 74, 14);

		contentPanel.add(lblWorkWebsite);

		contentPanel.add(workWebsiteTextField);
		lblWorkCountry.setBounds(10, 395, 74, 14);

		contentPanel.add(lblWorkCountry);
		workCountryComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		workCountryComboBox.setBounds(88, 392, 144, 20);

		contentPanel.add(workCountryComboBox);
		lblWorkState.setBounds(256, 395, 61, 14);

		contentPanel.add(lblWorkState);
		workStateComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		workStateComboBox.setBounds(318, 392, 152, 20);

		contentPanel.add(workStateComboBox);
		lblWorkCity.setBounds(480, 395, 57, 14);

		contentPanel.add(lblWorkCity);
		workCityComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		workCityComboBox.setBounds(543, 392, 144, 20);

		contentPanel.add(workCityComboBox);
		workGoogleTextField.setColumns(10);
		workGoogleTextField.setBounds(80, 470, 272, 20);

		contentPanel.add(workGoogleTextField);
		workTwitterTextField.setColumns(10);
		workTwitterTextField.setBounds(80, 445, 272, 20);

		contentPanel.add(workTwitterTextField);
		workFacebookTextField.setColumns(10);
		workFacebookTextField.setBounds(93, 420, 264, 20);

		contentPanel.add(workFacebookTextField);
		lblWorkFacebook.setBounds(10, 423, 81, 14);

		contentPanel.add(lblWorkFacebook);
		lblWorkTwitter.setBounds(10, 448, 69, 14);

		contentPanel.add(lblWorkTwitter);
		lblWorkGoogle.setBounds(10, 473, 68, 14);

		contentPanel.add(lblWorkGoogle);
		label_4.setBounds(354, 473, 46, 14);

		contentPanel.add(label_4);
		label_5.setBounds(354, 448, 46, 14);

		contentPanel.add(label_5);
		label_6.setBounds(360, 423, 46, 14);

		contentPanel.add(label_6);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							getData();
						} catch (DAOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	private void getData() throws DAOException, SQLException{
		Contact contact = new Contact();
		Contact getcontact;
		ContactDAOImpl contactDAOImpl = new ContactDAOImpl();
		Work work = new Work();
		Work getwork ;
		WorkDAOImpl workDAOImpl = new WorkDAOImpl();
		Online_account online_account = new Online_account();
		Online_accountDAOImpl online_accountDAOImpl = new Online_accountDAOImpl();
		Online_account getonline_account;
		Username username = new Username();
		UsernameDAOImpl usernameDAOImpl = new UsernameDAOImpl();
		Username getusername;
		Country country = new Country();
		State state = new State();
		City city = new City();
		
		{
			getwork = workDAOImpl.findById(work.getId());
			getonline_account = online_accountDAOImpl.findById(online_account.getId());
			getusername = usernameDAOImpl.findById(username.getId());
			contact.setFirst_name(firstNameTextField.getText());
			contact.setSecond_name(secondNameTextField.getText());
			contact.setThird_name(thirdNameTextField.getText());
			contact.setLast_name(lastNameTextField.getText());
			contact.setAddress(addressTextArea.getText());
			contact.setCity_id(cityComboBox.getSelectedIndex()+1);
			contact.setLand_phone(landPhoneTextField.getText());
			contact.setMobile_number(mobilePhoneTextField.getText());
			contact.setEmail(emailTextField.getText());
			contact.setWebsite(websiteTextField.getText());
			contact.setJob(jobTextField.getText());
			
			contact.setUsername_id(getusername.getId());

			
				Connection connection = Manager.getConnection();
				int maxIDForOnlineAcount = 0;
				Statement s2 = connection.createStatement();
				s2.execute("select max(ID) from online_account");
				ResultSet rs2 = s2.getResultSet();
				while (rs2.next()) {
					maxIDForOnlineAcount = rs2.getInt(1);
				}
				
			online_account.setFacebook(facebookTextField.getText());
			online_account.setTwitter(twitterTextField.getText());
			online_account.setGoogle(googleTextField.getText());
			
			online_accountDAOImpl.insert(online_account);
			
			
				
				int maxIDForWork = 0;
				Statement s3 = connection.createStatement();
				s3.execute("select max(ID) from work");
				ResultSet rs3 = s3.getResultSet();
				while (rs2.next()) {
					maxIDForWork = rs2.getInt(1);
				}
			
				work.setCompny_name(companyTextField.getText());
				work.setLand_phone(landPhoneTextField.getText());
				work.setMobile_phone(mobilePhoneTextField.getText());
				work.setEmail(emailTextField.getText());
				work.setWebsite(websiteTextField.getText());
				work.setCity_id(workCityComboBox.getSelectedIndex()+1);
				
				workDAOImpl.insert(work);
				
			contact.setOnline_account_id(maxIDForOnlineAcount + 1);	
			contact.setWork_id(maxIDForWork + 1);
			
							contactDAOImpl.insert(contact);

		}
	}
	
		private void initComponents() {
			try {

				countryComboBox.setItems(CountryDAOImpl.getInstance().findAll());
			} catch (Exception e) {
				e.printStackTrace();
			}
			countryComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Country country = (Country) countryComboBox.getModel()
							.getSelectedItem();
					countryComboBoxAction(country);
				}
			});

			stateComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (stateComboBox.getModel().getSelectedItem() != null) {
						State state = (State) stateComboBox.getModel()
								.getSelectedItem();
						stateComboBoxAction(state);
					}
				}
			});
			cityComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (cityComboBox.getModel().getSelectedItem() != null) {
						City city = (City) cityComboBox.getModel()
								.getSelectedItem();
						cityComboBoxAction(city);
					}
				}
			});
			try {

				workCountryComboBox.setItems(CountryDAOImpl.getInstance().findAll());
			} catch (Exception e) {
				e.printStackTrace();
			}
			workCountryComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Country country = (Country) workCountryComboBox.getModel()
							.getSelectedItem();
					workCountryComboBoxAction(country);
				}
			});

			workStateComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (workStateComboBox.getModel().getSelectedItem() != null) {
						State state = (State) workStateComboBox.getModel()
								.getSelectedItem();
						workStateComboBoxAction(state);
					}
				}
			});
			workCityComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (workCityComboBox.getModel().getSelectedItem() != null) {
						City city = (City) workCityComboBox.getModel()
								.getSelectedItem();
						workCityComboBoxAction(city);
					}
				}
			});
		}
		
		private void countryComboBoxAction(Country country) {
		try {
			cityComboBox.clear();
			stateComboBox.clear();
			stateComboBox.setItems(StateDAOImpl.getInstance()
					.findByCountry_id(country.getId()));
			stateComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void stateComboBoxAction(State state) {
		try {
			cityComboBox.clear();
			cityComboBox.setItems(CityDAOImpl.getInstance().findByState_id(
					state.getId()));
			cityComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void cityComboBoxAction(City city) {

	}

	private void workCountryComboBoxAction(Country country) {
		try {
			workCityComboBox.clear();
			workStateComboBox.clear();
			workStateComboBox.setItems(StateDAOImpl.getInstance()
					.findByCountry_id(country.getId()));
			workStateComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void workStateComboBoxAction(State state) {
		try {
			workCityComboBox.clear();
			workCityComboBox.setItems(CityDAOImpl.getInstance().findByState_id(
					state.getId()));
			workCityComboBox.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void workCityComboBoxAction(City city) {

	}
}
